from webutils.layers.services import ManagerProxy, Service

from .. import managers, models, const


class UnitService(Service):
    required_context = ['unit_id']
    nds_api = ManagerProxy(managers.NDSAPI)

    async def get_unit_info(self):
        raw_info = await self.nds_api.get_unit_info(self.unit_id)
        if str(raw_info.get('errcode')) == '12302':
            return None
        raw_info = raw_info['data']
        unit_type = const.UnitType(int(raw_info['type']))
        status = const.UnitStatus(int(raw_info['status']))
        unit_info = models.UnitInfo(
            id=raw_info['unit_id'],
            type=unit_type,
            course_id=raw_info['course_id'],
            status=status,
        )
        return unit_info
